package com.zerui.manager.mq;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zerui.manager.accessauthorg.domainservice.AccessAuthOrgRepository;
import com.zerui.manager.accessauthpersonnel.domainservice.AccessAuthPersonnelRepository;
import com.zerui.manager.common.mq.consumer.DepartmentOrPersonDeleteMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;

/**
 * 设备通行记录MQ
 *
 * @Author wh
 * @create_time 2023-05-11 17:10
 */
@Slf4j
@Component
@RocketMQMessageListener(consumerGroup = "${rocketmq.consumer.group}-person", topic = "person-org-delete-topic" )
public class DepartmentOrPersonDeleteRocketMqConsumer implements RocketMQListener<DepartmentOrPersonDeleteMessage> {

    @Resource
    private AccessAuthOrgRepository accessAuthOrgRepository;
    @Resource
    private AccessAuthPersonnelRepository accessAuthPersonnelRepository;

    @Override
    public void onMessage(DepartmentOrPersonDeleteMessage message) {

        log.info("person-org-delete-topic message receive start message:{}",message);

        String method = message.getMethod();

        if(StringUtils.isBlank(method) || CollectionUtils.isEmpty(message.getIds())){
            return;
        }
        List<Long> ids = message.getIds();

        if("ORG_DELETE".equals(method)){
            accessAuthOrgRepository.deleteByOrgId(ids);
        }else if("PERSON_DELETE".equals(method)){
            accessAuthPersonnelRepository.deleteByPersonIds(ids);
        }

    }

}
